package com.zjy.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Zhengjz
 * @version 1.0
 * @className UserController
 * @date 2021/1/12 15:46
 */
@Controller
public class UserController {



    @RequestMapping("/llogin")
    public String toLoing(){
        return "login";
    }
//登陆验证
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String ifLogin(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password) {


        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        //获取令牌
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);//执行登录
            return "redirect:/index";
        } catch (UnknownAccountException e) {
            request.setAttribute("message", "用户名或密码错误！");
            return "login";
        } catch (IncorrectCredentialsException e) {
            request.setAttribute("message", "用户名或密码错误！");
            return "login";
        }


    }

        @RequestMapping("/index")
        public String toIndex () {
            return "index";
        }


        @RequestMapping("/admin/list")
        public String adminList () {
            return "admin/admin-list";
        }

        /**
         * 会员管理
         *     |
         *     |
         *     》 统计页面
         */
        @RequestMapping("/welcome/welcome1")
        public String welcome1 () {
            return "welcome/welcome1";
        }


//        /**
//         * 会员管理
//         *     |
//         *     |
//         *     》 会员列表(静态表格)
//         */
//        @RequestMapping("/member/list")
//        public String memberList () {
//            return "member/member-list";
//        }


        /**
         * 会员管理
         *     |
         *     |
         *     》 会员列表(动态表格)
         */
        @RequestMapping("/member/list1")
        public String welcomedongtai () {
            return "member/member-list1";
        }


        /**
         * 会员管理
         *     |
         *     |
         *     》 会员删除
         */
        @RequestMapping("/member/delete")
        public String memberDelete () {
            return "member/member-del";
        }

//    /**
//     * 会员管理
//     *     |
//     *     |
//     *     》 会员等级管理
//     */
//    @RequestMapping("/member/list")
//    public String memberClass(){
//        return "member/member-list";
//    }


    //未经授权页面
    @RequestMapping("/natuch")
    public String natuch(){
            return "natuch";
    }



}
